<template>
	<view class="xc-wrap">

		<view class="xc-block">

			<uv-form labelPosition="left" :model="control.form" :rules="control.rules" ref="form">

				<uv-form-item label="当前岗位" borderBottom>
					<uv-input v-model="shenfen" border="none" readonly="readonly">
					</uv-input>
				</uv-form-item>


				<uv-form-item label="申请岗位" prop="shenfen" required borderBottom>
					<radio-group @change="handleRadioChange">
						<label class="radio-item" v-for="(item, index) in radioList" :key="index">
							<radio :value="item.value" :checked="item.checked" />
							{{ item.text }}
						</label>
					</radio-group>
				</uv-form-item>

				<uv-form-item label="姓名" prop="memo" required borderBottom>
					<uv-input v-model="control.form.memo" border="surround">
					</uv-input>
				</uv-form-item>
				
			
				<uv-form-item v-if="control.form.type == 3" label="证件照片(电工证等)" prop="control.form.diangongzheng"
					borderBottom>
					<xc-uv-upload v-model="control.form.diangongzheng" maxCount="1"></xc-uv-upload>

				</uv-form-item>

				<uv-form-item v-if="control.form.type == 3" label="证件到期时间" prop="memo" required borderBottom>

					<uv-datetime-picker ref="datetimePicker" v-model="value" mode="date" :formatter="formatter"
						@confirm="confirm1">
					</uv-datetime-picker>

					<uv-input v-model="control.form.zhengjian_daoqi" @tap="open" border="surround" disabled="true">
					</uv-input>
				</uv-form-item>


				<uv-form-item v-if="control.form.type == 3" label="所属省份" prop="control.form.province_id"
					borderBottom>
					<uv-picker ref="picker2" :columns="columns2" @confirm="confirm2"></uv-picker>
					<uv-input v-model="control.form.province_id" @tap="openPicker2" border="surround" disabled="true">
					</uv-input>
				</uv-form-item>

				<uv-form-item v-if="control.form.type == 3" label="所属分包单位" prop="control.form.company_id"
					borderBottom>
					<uv-picker ref="picker3" :columns="columns3" @confirm="confirm3"></uv-picker>

					<uv-input v-model="control.form.company_id" @tap="openPicker3" border="surround" disabled="true">
					</uv-input>
				</uv-form-item>

			</uv-form>
			<uv-button type="primary" text="提交审核" customStyle="margin-top: 10px" @tap="control_submit"></uv-button>

		</view>
	</view>
</template>

<script>
	import config from "@/config/config.js";
	import xcUniappUtils from '@/library/xcore/js/XcUniappUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	import xcCloginUtils from '@/library/xcore/js/XcCloginUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';

	export default {
		data() {
			return {
				value: Number(new Date()),
				columns2: [

				],
				columns3: [

				],
				gongsi: [],
				radioList: [{
						text: '区域负责人',
						value: 1,
						checked: false
					},
					{
						text: '项目经理',
						value: 2,
						checked: false
					},
					{
						text: '现场施工员',
						value: 3,
						checked: true
					}
				],
				selectedValue: 3, // 用于存储选中的值
				shenfen: '无',
				config: config,
				xsStringUtils: xsStringUtils,
				xsDateUtils: xsDateUtils,
				sp: {},
				sr: {},
				control: {
					form: {
						shenfen: null,
						diangongzheng: null,
						type: 3
					}
				}
			}
		},
		onLoad(options) {
			let that = this;
			that.xinit();
			that.getData();
		},
		onShow() {
			let that = this;
			xcUniappUtils.onShow(function() {
				that.public_show();
			});
		},
		onPullDownRefresh() {
			let that = this;
			setTimeout(function() {
				that.public_show();
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			open() {
				this.$refs.datetimePicker.open();
			},
		
			timestampToTime(timestamp) {
				let date = new Date(timestamp);
				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				let hours = date.getHours().toString().padStart(2, '0');
				let minutes = date.getMinutes().toString().padStart(2, '0');
				let seconds = date.getSeconds().toString().padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			openPicker() {
				this.$refs.picker.open();
			},
			confirm(e) {
				console.log('confirm', e);
			},
			confirm1(e) {
				this.control.form.zhengjian_daoqi = this.timestampToTime(e.value)
			},
			confirm2(e) {
				console.log('confirm', e);
				this.control.form.province_id = e.value[0]
			},
			confirm3(e) {
				console.log('confirm', e);
				this.control.form.company_id = e.value[0]
			},
		
			openPicker2() {
				this.$refs.picker2.open();
			},
			openPicker3() {
				this.$refs.picker3.open();
			},
			handleRadioChange(e) {
				this.control.form.type = e.detail.value;
			},
			xinit() {
				let that = this;
			},
			public_reset() {
				let that = this;
				that.public_show();
			},
			public_show() {
				let that = this;

				xcCloginUtils.getUserinfo(function(userinfo) {
					that.shenfen = '无'

					if (userinfo.shenfen == 1) {
						that.shenfen = '区域负责人'
						// that.radioList[0].checked = true
						// that.radioList[1].checked = false
						// that.radioList[2].checked = false
					}

					if (userinfo.shenfen == 2) {
						that.shenfen = '项目经理'
						// that.radioList[0].checked = false
						// that.radioList[1].checked = true
						// that.radioList[2].checked = false
					}

					if (userinfo.shenfen == 3) {
						that.shenfen = '现场施工员'
						// that.radioList[0].checked = false
						// that.radioList[1].checked = false
						// that.radioList[2].checked = true
						
					}
						
					// xcHttpUtils.row2form(userinfo, that.control.form);
				
				});
			},
			getData() {
				let that = this;
				xcHttpUtils.getXiala('', function(res2) {
					that.columns2[0] = res2.b
					that.columns3[0] = res2.a
					
					console.log(11111111,that.columns2[0])
				});
			
			},

			control_submit() {
				let that = this;
				let control = that.control;
				console.log(3333, control.form)
				if (!that.control.form.type) {
					uni.showToast({
						title: '请填写完整信息',
						icon: "none",
						duration: 2000
					});

					return
				}

				xcHttpUtils.shenfenshenhe(control.form, function(res2) {
					uni.showToast({
						title: res2,
						icon: "none",
						duration: 2000
					});
				});

			},
			control_clickDelete() {
				let that = this;
				xcViewUtils.router_gotoPage("/pages/xclogin/me_delete")
			},
			mp_control_changeNick(e) {
				let that = this;
				let control = that.control;
				control.form.nickname = e.detail.value;
			},
			mp_control_onChooseAvatar(e) {
				let that = this;
				let control = that.control;
				xcHttpUtils.upload(e.detail.avatarUrl, function(url) {
					control.form.avatar = url;
				});
			},
		},

	}
</script>

<style lang="scss">
	.xp-mp-avatar {
		position: absolute;

		.avatar-wrapper {
			padding: 0px;
		}
	}

	.container {
		padding: 20px;
	}

	.radio-item {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
	}
</style>
